package com.notecalendar.dao.impl;

import java.math.BigInteger;
import java.util.List;

import org.hibernate.Query;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.notecalendar.dao.NotesDao;
import com.notecalendar.model.Notes;

/**
 * Class for implementing the definite methods in bean NoteDao 
 * @author angelcereijo
 *
 */
public class NotesDaoImpl extends HibernateDaoSupport implements NotesDao {

	
	/**
	 * @see NotesDao#addNote(Notes)
	 */
	public Notes addNote(Notes notes) {
		notes.setId(getId());
		getHibernateTemplate().save(notes);
		return notes;
	}

	/**
	 * @see NotesDao#delelteNote(Notes)
	 */
	public void delelteNote(Notes notes) {
		getHibernateTemplate().delete(notes);
	}
	
	/**
	 * @see NotesDao#getNotes()
	 */
	@SuppressWarnings("unchecked")
	public List<Notes> getNotes() {
		return getHibernateTemplate().find("from Notes");
	}
	
	/**
	 * Get the next id for one note
	 * @return
	 */
	private BigInteger getId(){
		
		Query query = getSession().getNamedQuery("notesMaxId");
		BigInteger id = (BigInteger)query.list().get(0);
		id = (id==null)?new BigInteger("0"):id;
		
		return id;	
	}

}
